package com.ebt.m.policy.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.commons.buscomponent.listview.IView;
import com.ebt.m.commons.utils.Logger;
import com.ebt.m.data.rxModel.apibean.PolicyProposal;
import com.ebt.m.data.rxModel.apibean.PolicyProposalItem;
import com.ebt.m.utils.DateUtils;
import com.ebt.m.utils.NumberHelper;
import com.ebt.m.utils.imageloader.ImageLoader;
import com.ebt.m.utils.imageloader.ImageLoaderUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SearchProposalItemView extends IView implements View.OnClickListener {

    private Context mContext;
    @BindView(R.id.ll_content)
    LinearLayout ll_content;
    @BindView(R.id.tv_import)
    TextView tv_import;
    @BindView(R.id.tv_date)
    TextView tv_date;
    @BindView(R.id.tv_coverage)
    TextView tv_coverage;
    @BindView(R.id.tv_premium)
    TextView tv_premium;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_personame)
    TextView tv_personame;
    @BindView(R.id.iv_image)
    ImageView iv_image;
    PolicyProposalItem item;
    //投保人姓名
    String applicantName;

    public SearchProposalItemView(Context context) {
        this(context, null);
    }

    @Override
    public void update(Object... args) {
        try {
            PolicyProposal proposalsBean = (PolicyProposal) args[0];
            applicantName = (String) args[1];
            tv_personame.setText(proposalsBean.name);
            if (proposalsBean.proposalItems != null && proposalsBean.proposalItems.size() >= 1) {
                item = (PolicyProposalItem) proposalsBean.proposalItems.get(0);
                tv_name.setText(item.name);
                tv_date.setText(DateUtils.dateTime2String(DateUtils.stringToDateTime(item.createTime), DateUtils.MM_DD_HH_MM_SS));
                tv_coverage.setText("保额" + NumberHelper.formatDouble(Double.valueOf(item.coverage)).toString() + "元");
                //处理脏数据，有的带"元"，有的不带"元"
                if (item.premium.contains("元")) {
                    item.premium = item.premium.replace("元", "");
                }
                tv_premium.setText("保费" + NumberHelper.formatDouble(Double.valueOf(item.premium)).toString() + "元");
                ImageLoaderUtil.getInstance().loadImage(getContext(), new ImageLoader.Builder().url(item.thumbnail).imgView(iv_image).build());
            }
        } catch (Exception e) {
            Logger.e(e);
        }
    }

    public SearchProposalItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SearchProposalItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
        View contentView = inflate(context, R.layout.view_policy_proposal_item_with_name, this);
        ButterKnife.bind(this);
        tv_import.setOnClickListener(this);
        ll_content.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (mOnclickListner != null) {
            mOnclickListner.onClick(v, item, applicantName);
        }
    }
}